package local_database;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

import updater.currency.convertor.ReadRSS;
import updater.currency.data.Currency;
import updater.currency.data.CurrencyName;
import fxTrader.retriever.CurrencyRetriever;

public class Statistic {
	XmlReader reader;
	XmlWriter writer;

	public Statistic(String xmlName) {
		reader = new XmlReader(xmlName);
		writer = new XmlWriter(xmlName);
	}

	public void process() {
		String option;
		int options = 0;
		Scanner scanner = new Scanner(System.in);

		while (true) {
			System.out
					.println("-------------[My Transaction Records]------------");
			System.out.println("| [1] : My account				|");
			System.out.println("| [2] : Max profit				|");
			System.out.println("| [3] : exit					|");
			
			option = scanner.next();
			
			try {
				options = Integer.parseInt(option);
				switch (options) {
				case 1:
					userAccountStat();
					break;
				case 2:
					calMaxProfit();
					break;
				case 3:
					exit();
					break;
				default:
					System.out
					.println("!!Warning : You are allowed to input 1, 2 and 3 only!!");
					System.out.println();
					break;
				}
			} catch (Exception e) {
				System.out
						.println("!!Warning : You are allowed to input 1, 2 and 3 only!!");
				System.out.println();
			}
			if (options == 3)
				break;
		}
	}

	private void userAccountStat() {
		String[] currency;
		ArrayList currenciesList;
		currency = getCurrencyCodeList();

		String currencyCode, price, time;
		System.out.println("--------------[My Account Statistic]-------------");
		for (int i = 0; i < currency.length; i++) {
			currencyCode = currency[i];
			price = reader.getNodeTextContent("Currency", "code", currencyCode,
					"buy_price");
			time = reader.getNodeTextContent("Currency", "code", currencyCode,
					"buy_time");
			System.out.println("Currency : " + currencyCode);
			System.out.println("Price : " + price);
			System.out.println("Buy Time : " + time);
			System.out.println();
		}

	}

	private void calMaxProfit() {
		System.out.println("-----------[Maximize Profit Analysis]------------");

		double currencyBuyPrice[];
		double currencyUpdatePrice[];
		String currencyCode[];
		currencyCode = getCurrencyCodeList();
		currencyBuyPrice = new double[currencyCode.length];
		currencyUpdatePrice = new double[currencyCode.length];
		String code;
		for (int i = 0; i < currencyCode.length; i++) {
			code = currencyCode[i];
			currencyBuyPrice[i] = Double.parseDouble(reader.getNodeTextContent(
					"Currency", "code", code, "buy_price"));
			currencyUpdatePrice[i] = getCurrencyUpdatedPrice(code);
		}

		boolean isProfitable = false;
		ArrayList analyzedCurrency = new ArrayList();
		ArrayList<String> analyzedCurrencyCode = new ArrayList<String>();

		for (int i = 0; i < currencyBuyPrice.length; i++) {
			if (currencyBuyPrice[i] < currencyUpdatePrice[i]) {
				analyzedCurrencyCode.add(currencyCode[i]);
				double percentage = ((currencyUpdatePrice[i] - currencyBuyPrice[i]) / currencyBuyPrice[i]) * 100;
				analyzedCurrency.add(percentage);
				isProfitable = true;
			}
		}

		if (isProfitable) {
			System.out.println("You should sell : ");
			for (int i = 0; i < analyzedCurrencyCode.size(); i++) {
				System.out.print(analyzedCurrencyCode.get(i) + " : ");
				System.out.printf("%1$.2f", analyzedCurrency.get(i));
				System.out.print(" % ");
				System.out.println();
			}

			System.out.println();
		}

	}

	private String[] getCurrencyCodeList() {
		String[] currencyCode;
		ArrayList currenciesList;
		XmlReader reader = new XmlReader("UserStat.xml");
		currenciesList = reader.getNodesAttrContent("Currency", "code");
		currencyCode = new String[currenciesList.size()];
		for (int i = 0; i < currenciesList.size(); i++) {
			currencyCode[i] = (String) currenciesList.get(i);
		}
		return currencyCode;
	}

	private void exit() {
		System.out.println("back to main menu...");
	}

	private double getCurrencyUpdatedPrice(String code) {
		String cur;
		double price = 0;
		ReadRSS rs = new ReadRSS();
		ArrayList<CurrencyName> currencyNameList = rs
				.GetCurrencyNameList("currency_list");

		boolean isCorrectCurrency = false;

		cur = code;

		for (int i = 0; i < currencyNameList.size(); i++) {

			if (cur.equalsIgnoreCase(currencyNameList.get(i).getSymbol())) {
				isCorrectCurrency = true;
			}
		}

		if (isCorrectCurrency) {
			Currency currency = CurrencyRetriever
					.getLatestCurrencyBySymbol(cur);
			price = currency.getPrice();
		}
		return price;
	}

}
